/**
 * $Revision: 1.0 $
 * $Author: Eric Yang $
 * $Date: Aug 7, 2009 10:45:12 PM $
 *
 * Author: Eric Yang
 * Date  : Aug 7, 2009 10:45:12 PM
 *
 */
package com.gooex.service.organization.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.c4j.mail.SpringMailSender;
import org.c4j.string.StringTools;
import org.c4j.system.web.hibernate.BaseHibernateService;

import com.gooex.domain.organization.User;
import com.gooex.domain.organization.UserStatus;
import com.gooex.service.organization.AuthorityContext;
import com.gooex.service.organization.IUserManager;
import com.gooex.service.organization.exception.UserException;


/**
 * @author Eric Yang
 * @version 1.0
 */
public class UserManagerImpl extends BaseHibernateService implements IUserManager {

	private static final Log logger = LogFactory.getLog(UserManagerImpl.class);

	private SpringMailSender mailSender;

	public User getUser(String id) {
		if (StringTools.isBlankOrNull(id))
			return null;
		return (User) getHibernateTemplate().get(User.class, id);
	}

	public List<User> getUsers() {
		return getHibernateTemplate().find("from User");
	}

	public User getUserByAccount(String email) {
		String hql = "from " + User.class.getName() + " where email=?";
		List<User> users = getHibernateTemplate().find(hql, email);
		if (users != null && users.size() == 1)
			return users.get(0);
		return null;
	}

	public boolean validateUserInfo(User user) {
		try {
			checkUserInfo(user);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public void checkUserInfo(User user) throws UserException {
		if (user == null)
			throw new UserException("user.not.existed");
		if (StringTools.isBlankOrNull(user.getEmail()))
			throw new UserException("user.email.null.error");

		List<Object> params = new ArrayList<Object>();
		String hql = "select count(*) from " + User.class.getName() + " where  email=?";
		params.add(user.getEmail());

		if (StringTools.isNotBlankOrNull(user.getId())) {
			hql += " and id<>?";
			params.add(user.getId());
		}
		int count = findHQLQueryCount(hql, params.toArray());
		if (count > 0)
			throw new UserException("user.duplicated");
	}

	public void saveUser(User user) throws UserException {
		boolean isRegister = false;
		if (StringTools.isBlankOrNull(user.getId())) {
			user.setCreationDate(new Date());
			user.setStatus(UserStatus.LOCKED);
			user.setId(null);
			isRegister = true;
		}
		checkUserInfo(user);
		getHibernateTemplate().saveOrUpdate(user);
		if (isRegister) {
			// TODO
			mailSender.sendMail(new String[] { user.getEmail() }, null, "register confirm", "welcome");
		}
	}

	public void removeUser(User user) throws UserException {
		user = getUser(user.getId());
		if (AuthorityContext.getSuperUser().equals(user)) {
			logger.debug("User[" + user.getEmail() + "] is super user, can't be removed!");
			throw new UserException("Can't remove super user!");
		}
		getHibernateTemplate().delete(user);
	}

	public void setMailSender(SpringMailSender mailSender) {
		this.mailSender = mailSender;
	}
}
